﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CircuitBoardApplication.Classes
{
    class ComponentMain
    {
        private int id;
        private String type;
        private int value;
        private int x;
        private int y;
        public static int radius = 40;
        private List<ComponentMain> neighbors;
        private List<Connection> connectionList;


        public ComponentMain(int id, String type, int value, int x, int y)
        {
            this.id = id;
            this.type = type;
            this.value = value;
            this.x = x;
            this.y = y;
            this.neighbors = new List<ComponentMain>();
            this.connectionList = new List<Connection>();
        }

        public int ID
        {
            get { return this.id; }
            set { this.id = value; }
        }
        public String Type
        {
            get { return this.type; }
            set { this.type = value; }
        }
        public int Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
        public int X
        {
            get { return this.x; }
            set { this.x = value; }
        }
        public int Y
        {
            get { return this.y; }
            set { this.y = value; }
        }
        /*public List<ComponentMain> Neighbors
        {
            get { return this.neighbors; }
        }*/
        public List<Connection> Connections
        {
            get
            {
                return this.connectionList;
            }
        }

        /// <summary>
        /// If this plate has a neighbor with identity id,
        /// then that neighbor is returned,
        /// else null is returned
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /*public ComponentMain GetNeighbor(int id)
        {
            foreach (ComponentMain n in this.neighbors)
            {
                if (n.ID == id) { return n; }
            }
            return null;
        }*/

        public void addConnection(ComponentMain startPoint, ComponentMain endPoint)
        {
            this.connectionList.Add(new Connection(startPoint, endPoint));
        }
        public void removeConnection(Connection removeconn)
        {
            foreach (Connection conn in connectionList)
            {
                if (conn == removeconn)
                    connectionList.Remove(conn);
            }
        }

        

        /// <summary>
        /// adds plate p to its neighbors
        /// </summary>
        /// <param name="p"></param>
        /*public void AddNeighbor(ComponentMain c)
        {
            this.neighbors.Add(c);
        }*/

        /// <summary>
        /// checks if the point (xmouse,ymouse) is on this plate.
        /// In other words: it checks if the distance between the center of this plate and the point (xmouse,ymouse)
        /// is less than or equal to the radius.
        /// </summary>
        /// <param name="xmouse"></param>
        /// <param name="ymouse"></param>
        /// <returns></returns>
        public bool ContainsPoint(int xmouse, int ymouse)
        {
            return (this.x - xmouse) * (this.x - xmouse) + (this.y - ymouse) * (this.y - ymouse) <= (radius * radius + radius * radius) * 3;
        }

        public virtual void DrawItself(System.Drawing.Graphics gr, System.Windows.Forms.ImageList il)
        {
            int diameter = 2 * ComponentMain.radius;
            gr.DrawImage(il.Images[3], this.X - ComponentMain.radius, this.Y - ComponentMain.radius, diameter, diameter);
        }

        public virtual void setInputsFromConnections(Connection conn)
        {
        }

        public override string ToString()
        {
            return this.id + " Type: " + GetType().Name.ToString() + " Output value: " + Value.ToString();
        }
    }
}
